package com.douyin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.douyin.dto.PageActionsDTO;
import com.douyin.dto.Result;
import com.douyin.entity.Favorites;
import com.douyin.entity.Likes;
import com.douyin.entity.Videos;
import com.douyin.mapper.FavoritesMapper;
import com.douyin.mapper.VideosMapper;
import com.douyin.service.FavoritesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.douyin.utils.BaseContext;
import com.douyin.vo.FavoritesVO;
import com.douyin.vo.UserActionVideoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 视频收藏表 服务实现类
 * </p>
 *
 * @author k
 * @since 2025-07-27
 */
@Service
public class FavoritesServiceImpl extends ServiceImpl<FavoritesMapper, Favorites> implements FavoritesService {
    @Autowired
    FavoritesMapper favoritesMapper;
    @Autowired
    VideosMapper videoMapper;
    @Override
    public Result favoritesOrNot(Long videoId) {
        //判断videoId是否存在
        if (videoId == null) {
            return Result.fail("videoId不能为空");
        }
        Videos videos = videoMapper.selectById(videoId);
        if (videos == null) {
            return Result.fail("视频不存在");
        }
        //判断是否登录
        if (BaseContext.getCurrentId() == null) {
            return Result.fail("请先登录");
        }
        //判断是否收藏过
        LambdaQueryWrapper<Favorites> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Favorites::getVideoId, videoId).eq(Favorites::getUserId, BaseContext.getCurrentId());
        Favorites favorites = favoritesMapper.selectOne(queryWrapper);
        if (favorites == null) {
            //未收藏，收藏
            Favorites favorites1 = new Favorites();
            favorites1.setVideoId(videoId);
            favorites1.setUserId(BaseContext.getCurrentId());
            favorites1.setCreatedAt(LocalDateTime.now());
            favorites1.setStatus(1);
            favoritesMapper.insert(favorites1);
            FavoritesVO favoritesVO = new FavoritesVO();
            BeanUtils.copyProperties(favorites1, favoritesVO);
            Integer favoritesCount = getFavoritesCount(videoId);
            favoritesVO.setFavoritesCount(favoritesCount);
            return Result.ok(favoritesVO);
        }
        if (favorites.getStatus() == 1) {
            //取消收藏
            favorites.setStatus(0);
            favoritesMapper.updateById(favorites);
            FavoritesVO favoritesVO = new FavoritesVO();
            BeanUtils.copyProperties(favorites, favoritesVO);
            Integer favoritesCount = getFavoritesCount(videoId);
            favoritesVO.setFavoritesCount(favoritesCount);
            return Result.ok(favoritesVO);
        }
        if (favorites.getStatus() == 0) {
            //收藏
            favorites.setStatus(1);
            favoritesMapper.updateById(favorites);
            FavoritesVO favoritesVO = new FavoritesVO();
            BeanUtils.copyProperties(favorites, favoritesVO);
            Integer favoritesCount = getFavoritesCount(videoId);
            favoritesVO.setFavoritesCount(favoritesCount);
            return Result.ok(favoritesVO);
        }
        return Result.fail("收藏状态异常");

    }

    @Override
    public Result getFavorites(PageActionsDTO pageActionsDTO) {
        PageActionsDTO pageActionsDTO1 = checkFollowsDTO(pageActionsDTO);
        if (pageActionsDTO1 == null) {
            return Result.fail("参数错误");
        }
        IPage<UserActionVideoVO> favoritesPage = getFavoritesPage(pageActionsDTO1);
        return Result.ok(favoritesPage);
    }

    private Integer getFavoritesCount(Long videoId){
        LambdaQueryWrapper<Favorites> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Favorites::getVideoId, videoId).eq(Favorites::getStatus, 1);
        Integer count = favoritesMapper.selectCount(queryWrapper);
        return count;
    }
    public IPage<UserActionVideoVO> getFavoritesPage(PageActionsDTO queryDTO) {
        // 1. 创建 Mybatis-Plus 的 Page 对象
        IPage<UserActionVideoVO> page = new Page<>(queryDTO.getPageCount(), queryDTO.getPageSize());

        // 2. 调用 Mapper 的自定义方法，分页插件会自动生效
        IPage<UserActionVideoVO> resultPage = favoritesMapper.findUserFavoritesPage(page, queryDTO.getId());

        return resultPage;
    }
    private PageActionsDTO checkFollowsDTO(PageActionsDTO pageActionsDTO) {
        if (pageActionsDTO == null) {
            return null;
        }
        if (pageActionsDTO.getId() == null&&BaseContext.getCurrentId() == null) {
            return null;
        }
        if (BaseContext.getCurrentId() == null) {
            return null;
        }
        if (pageActionsDTO.getId() == null) {
            pageActionsDTO.setId(BaseContext.getCurrentId());
        }
        if (pageActionsDTO.getPageCount() == null) {
            pageActionsDTO.setPageCount(1);
        }
        if (pageActionsDTO.getPageSize() == null) {
            pageActionsDTO.setPageSize(10);
        }
        return pageActionsDTO;
    }
}